package controllerLayer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import modelLayer.*;
import DBLayer.*;


public class SalesOrderController {

	private static ProductController prCtr = new ProductController();
	private static SupplierController supCtr = new SupplierController();
	private static CustomerController cCtr = new CustomerController();
	
	
	private static IFDBInvoice dbI = new DBInvoice();
	private static IFDBSalesOrder dbS = new DBSalesOrder();
	private static IFDBPartSalesOrder dbPso = new DBPartSalesOrder();
	private SalesOrder soObj;

	
	

	//interaction 1
	//return list of all Products
	public List<Product> getAllProducts(boolean retriveAssociation)
	{
		return prCtr.getAllProducts(retriveAssociation);
	}

	//interaction 2
	//Create SalesOrder
	public SalesOrder createSalesOrder()
	{
		soObj = new SalesOrder();

		return soObj;
	}
	
	public SalesOrder getSalesOrder()
	{
		return soObj;
	}
	

	//interacation 3
	//Create PartSalesOrder, addPartSalesOrder(Product) to SalesOrderList 
	public PartSalesOrder selectProduct(Product p, int noOfItems)
	{		
		PartSalesOrder psoObj = new PartSalesOrder(p, noOfItems);
		soObj.addPartSalesOrder(psoObj);

		return psoObj;
	}

	public PartSalesOrder updateNoOfItems(PartSalesOrder psoObj, int noOfItems)
	{
		psoObj.setNoOfItems(noOfItems);
		return psoObj;
	}


	//iteraction 5
	//create customer, insert customer in db, add customer id, sets SalesOrders Customer, return Customer
	public Customer createCustomer(String fname, String lname, String address,
			String addressNo, String addressFloor, String zipCode,
			String countryCode, String phoneNo, String email, boolean retriveAssociation)
	{

		Customer cObj = cCtr.createCustomer(fname, lname, address, 
				addressNo, addressFloor, zipCode, countryCode, phoneNo, email);

		cObj.setCustomerID(String.valueOf(cCtr.insertCustomer(cObj, retriveAssociation)));

		if(soObj != null)
		{
			soObj.setCostumer(cObj);
		}
		return cObj;
	}
	
	public List<Customer> getAllCustomer()
	{
		return cCtr.getAllCustomer();
	}

	//interaction 6
	public Invoice closeOrder()
	{
		Invoice iObj = new Invoice();
		
		//interaction 6.1
		iObj = createInvoice();
		//interaction 6.2
		iObj = insertInvoice(iObj);
		
		if(iObj != null)
		{
		//interaction 6.3
		soObj.setInvoice(iObj);
		
		//interaction 6.4
		insertSalesOrder();
		
		//interaction 6.5
		insertPartSalesOrder();		
		}
		
		return iObj;

	}
	
	
	//interaction 6.4
	public void insertSalesOrder()
	{
		

		try{
			if(dbS.insertSalesOrder(soObj, false) == 1)
			{
				int soID = GetMax.getMaxId("select max(salesOrderID) FROM SalesOrder");

				soObj.setSalesOrderID(String.valueOf(soID));
			}
		}
		catch(Exception e)
		{
			p("closeOrder error, insert SalesOrder: "+e);
		}
	
	}
	
	
	//interaction 6.5 insertPartSalesOrder()
	public void insertPartSalesOrder()
	{
		List<PartSalesOrder> list = soObj.getList();
		
		for(PartSalesOrder p : list)
		{
			try{
				if(dbPso.insertPartSalesOrder(p, soObj, false) == 1)
				{
					int psoID = GetMax.getMaxId("select max(partSalesOrderID) FROM PartSalesOrder");

					p.setPartSalesOrderID(String.valueOf(psoID));
				}
			}
			catch(Exception e)
			{
				p("closeOrder error, insert PartSalesOrder: "+e);
			}
		}
				
	}
	


	//Invoice Controller
	
	//interaction 6.1
	public Invoice createInvoice()
	{
		Invoice iObj = new Invoice();
		
		
		java.util.Date utilDate = new java.util.Date();
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

		try{
			iObj.setPaymentDate(sqlDate);
			iObj.setAmount(soObj.calculateTotalAmount());
			p("create of Invoice successed: "+iObj);
		}
		catch(Exception e)
		{
			p("create of Invoice error: "+e);
		}
		return iObj;
	}

	
	//interaction 6.2
	public Invoice insertInvoice(Invoice iObj)
	{
	

		try{
			if(dbI.insertInvoice(iObj, false) == 1)
			{
				int invoiceID = GetMax.getMaxId("select max(invoiceID) FROM Invoice");

				iObj.setInvoiceID(String.valueOf(invoiceID));
			}
		}
		catch(Exception e)
		{
			p("closeOrder error: "+e);
		}


		return iObj;
	}
	
	
	
	
	//Supplier
	
	
	public List<Supplier> getAllSupplier()
	{
		return supCtr.getAllSupplier();
	}
	
	
	//Product
	public Product createProduct(String name, double purchasePrice, double salesPrice,
			double rentPrice, String countryOfOrigin, int minStock,
			String supplierName, String type, boolean retriveAssociation)
	{
		Supplier supplier = supCtr.searchName(supplierName, false);
		
		Product prObj = prCtr.createProduct(name, purchasePrice, salesPrice, rentPrice, 
												countryOfOrigin, minStock, supplier, type);

		prObj.setProductID(String.valueOf(prCtr.insertProduct(prObj, retriveAssociation)));

		
		return prObj;
	}


	//lazy printMethod
	public void p(String e)
	{
		System.out.println(e);
	}

}
